<?php
	//Initial date: 2009-05-13
	//Last change: 2009-05-30 by Kamil Jaskiewicz
	
	include("classes/DBTable.class.php");
	include("classes/projects.class.php"); 
	include("classes/companies.class.php"); 
	include("classes/departments.class.php");

	$module_name="Projects";
	$action=$_REQUEST['action'];
	$projects=new projects();

	if ($_REQUEST['submit'])
	{
		$id		= $_REQUEST['id'];
		$p_name = $_REQUEST['project_name'];
		$p_desc	= $_REQUEST['project_description'];
		$p_c_id	= $_REQUEST['project_company_id'];
		$p_sd	= $_REQUEST['project_start_date'];
		$p_ed	= $_REQUEST['project_end_date'];
		$p_s	= $_REQUEST['status'];
		$p_pc	= $_REQUEST['project_percent_complete'];
		$p_pr	= $_REQUEST['priority'];
		$p_t	= $_REQUEST['type'];

		if ($action == 'edit')
		{
			$projects->DB->query("UPDATE projects SET project_name='$p_name', project_description='$p_desc', 
				project_company_id='$p_c_id', project_start_date='$p_sd', project_end_date='$p_ed', project_status='$p_s', 
				project_percent_complete='$p_pc', project_priority='$p_pr', project_type='$p_t'
				WHERE project_id='$id'");

		} 
		else if ($action == 'add')
		{
			$q = "INSERT INTO projects VALUES ('', '$p_name', '$p_desc', '$p_c_id', '$p_sd', '$p_ed', '$p_s', '$p_pc', '$p_pr', '$p_t')";
			$projects->DB->query($q);			
			$id = $projects->DB->LastID;
		}
		else if($action == 'add_dept')
		{
			$d_id = $_REQUEST['dept_id'];
			$department = new departments();
			$projects->findID($id);
			$department->findID($d_id);
			if ($department->dept_company_id != $projects->project_company_id)
			{
				$projects->DB->query("UPDATE projects SET project_company_id='$department->dept_company_id' WHERE project_id='$id'");
			}
			$projects->DB->query("INSERT INTO project_departments VALUES('$id', '$d_id')");
		}
		header("location:projects.php?action=view&id=$id");
		exit();
	}

	if ($action == 'delete')
	{
		$id = $_REQUEST['id'];
		if($id)
		{
			$projects->delete($id);
			header("location:projects.php");
			exit();
		}
		$dept_id = $_REQUEST['dept_id'];
		if ($dept_id )
		{
			$p_id = $_REQUEST['p_id'];
			$projects->deleteDeptForProject($dept_id, $p_id);
			header("location:projects.php?action=view&id=$p_id");
			exit();
		}
	}


	include("header.php");
	

	//action not defined, display all projects
	if (!$action)
	{
		$projects->selectAll();

		print("<table id='grid'>");
		print("<tr><td><b>Project</b></td><td><b>Project&nbsp;status</b></td><td colspan='3'></td></tr>\n");
		while($projects->getNext())
		{
			print("<tr>");
			print("<td><a href='projects.php?action=view&id=$projects->project_id'>".str_replace(" ", "&nbsp;", $projects->project_name)."</a></td>");
			print("<td>".str_replace(" ", "&nbsp;", $projects->status)."</td>");
			print("<td><a href='projects.php?action=edit&id=$projects->project_id'>Edit</a></td>");
			print("<td><a href='projects.php?action=delete&id=$projects->project_id' onclick='return confirmation(\"Are you sure you want to delete this project (all tasks and departments assigned to this project also be deleted)?\");' >Delete</a></td>");
			print("<td style='width:80%'>&nbsp;</td>");
			print("</tr>\n\n");
		}
		print("<tr><td colspan='5' style='text-align:right'><a href='projects.php?action=add'>Add new project</a></td></tr>");
		print("</table>");
	}


	//display details of the project
	if ($action == 'view')
	{
		$id=$_REQUEST['id'];
		$projects->findID($id);

		print("<table id='grid'>");
		print("<tr><td><b>Name</b></td><td>$projects->project_name</td></tr>\n");
		print("<tr><td><b>Description</b></td><td>$projects->project_description</td></tr>\n");
		print("<tr><td><b>Company</b></td><td>$projects->company_name</td></tr>\n");
		print("<tr><td><b>Start Date</b></td><td>$projects->project_start_date</td></tr>\n");
		print("<tr><td><b>End Date</b></td><td>$projects->project_end_date</td></tr>\n");
		print("<tr><td><b>Status</b></td><td>$projects->status</td></tr>\n");
		print("<tr><td><b>Percent Complete</b></td><td>$projects->project_percent_complete</td></tr>\n");
		print("<tr><td><b>Priority</b></td><td>$projects->priority</td></tr>\n");
		print("<tr><td><b>Type</b></td><td>$projects->type</td></tr>\n");
		print("<tr><td colspan='2'><a href='projects.php?action=edit&id=$projects->project_id'>Edit</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='projects.php?action=delete&id=$projects->project_id' onclick='return confirmation(\"Are you sure you want to delete this project (all tasks and departments assigned to this project also be deleted)?\");' >Delete</a></td></tr>\n");
		print("</table>");

		$projects->selectDepartmentsForProject($id);
		print("<h3>Departments working on project</h3>");
		print("<table id='grid'>");
		print("<tr><td><b>Name</b></td><td></td></tr>\n");
		while($projects->getNext())
		{
			print("<tr>");
			print("<td>$projects->dept_name</td>");
			print("<td><a href='projects.php?action=delete&dept_id=$projects->dept_id&p_id=$id'>Delete</a></td>");
			print("</tr>\n\n");
		}
		print("<tr><td colspan='2'><a href='projects.php?action=add_dept&id=$id'>Add department</a></td></tr>\n");
		print("</table>");

	}

	if ($action == 'add_dept')
	{
		$id=$_REQUEST['id'];
		print("<form action='projects.php' method='post'>");		
		print("<input type='hidden' name='id' value='$id' />");
		print("<input type='hidden' name='action' value='add_dept' />");
		print("<input type='hidden' name='submit' value='true' />");
		print("<b>Department: </b>");
		print("<select name='dept_id'>\n");
		$dept = new departments();
		$dept->selectAll();
		while($dept->getNext())
		{
			print("<option value='$dept->dept_id'>$dept->dept_name in $dept->company_name</option>\n");
		}
		print ("</select>\n");		
		print ("<p></p><input type='submit' value='Submit' />&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' value='Cancel' onclick='history.back();' />");
		print("</form>");
	}

	if ($action == 'edit' || $action == 'add')
	{
		$id=$_REQUEST['id'];
		if ($id)
			$projects->findID($id);
		$project = new projects();
		$company = new companies();
		print("<form action='projects.php' method='post'>");
		if ($action == 'edit')
		{
			print("<input type='hidden' name='action' value='edit' />");
			print("<input type='hidden' name='id' value='$id' />");
		}
		else if ($action == 'add')
		{
			print("<input type='hidden' name='action' value='add' />");
		}
		print("<input type='hidden' name='submit' value='true' />");
		print("<table id='grid'>");
		print("<tr><td><b>Name</b></td><td><input type='text' value='$projects->project_name' name='project_name'/></td></tr>\n");
		print("<tr><td><b>Description</b></td><td><textarea name='project_description'>$projects->project_description</textarea></td></tr>\n");
		
		print("<tr><td><b>Company</b></td><td><select name='project_company_id'>\n");
		$company->selectAll();
		while($company->getNext())
		{
			if ($company->company_id != $projects->project_company_id)
				print("<option value='$company->company_id'>$company->company_name</option>\n");
			else
				print("<option selected value='$company->company_id'>$company->company_name</option>\n");
		}
		print ("</select></td></tr>\n");

		print("<tr><td><b>Start Date</b></td><td><input type='text' value='$projects->project_start_date' name='project_start_date'/>&nbsp;yyyy-mm-dd</td></tr>\n");
		print("<tr><td><b>End Date</b></td><td><input type='text' value='$projects->project_end_date' name='project_end_date'/>&nbsp;yyyy-mm-dd</td></tr>\n");
		print("<tr><td><b>Status</b></td><td><select name='status'>\n");
		$project->getPossibleStatusValues();
		while($project->getNext())
		{
			if ($project->dict_id != $projects->project_status)
				print("<option value='$project->dict_id'>$project->name</option>\n");
			else
				print("<option selected value='$project->dict_id'>$project->name</option>\n");
		}
		print ("</select></td></tr>\n");
		print("<tr><td><b>Percent Complete</b></td><td><input type='text' value='$projects->project_percent_complete' name='project_percent_complete'/></td></tr>\n");
		print("<tr><td><b>Priority</b></td><td><select name='priority'>\n");
		
		$project->getPossiblePriorityValues();
		while($project->getNext())
		{
			if ($project->dict_id != $projects->project_priority)
				print("<option value='$project->dict_id'>$project->name</option>\n");
			else
				print("<option selected value='$project->dict_id'>$project->name</option>\n");
		}		
		print("</select></td></tr>\n");
		print("<tr><td><b>Type</b></td><td><select name='type'>");

		$project->getPossibleTypeValues();
		while($project->getNext())
		{
			if ($project->dict_id != $projects->project_type)
				print("<option value='$project->dict_id'>$project->name</option>\n");
			else
				print("<option selected value='$project->dict_id'>$project->name</option>\n");
		}		
		print("</select></td></tr>\n");

		print("<tr><td colspan='2'><input type='submit' value='Submit' />&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' value='Cancel' onclick='history.back();' /></td></tr>\n");
		print("</table>");
		print("</form>");
	}
	
	include("footer.php");
?>
